package com.library.admin.modules.userrole.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.library.admin.modules.user.bo.UserUpdate;
import com.library.admin.modules.user.service.UserService;
import com.library.admin.modules.userrole.entity.UserRole;
import com.library.admin.modules.userrole.vo.UserRoleVO;
import com.library.admin.modules.userrole.bo.UserRolePage;
import com.library.admin.modules.userrole.bo.UserRoleInsert;
import com.library.admin.modules.userrole.bo.UserRoleUpdate;
import com.library.admin.modules.userrole.mapper.UserRoleMapper;
import com.library.admin.modules.userrole.struct.UserRoleStructMapper;
import com.library.admin.modules.userrole.service.UserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import com.library.common.util.PageCovertUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author xyh
 * @Desc 用户和角色关联表(UserRole)表服务实现类
 * @Date 2023-10-02 15:09:36
 */

@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Resource
    private UserRoleStructMapper userRoleStructMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Lazy
    @Resource
    private UserService userService;


    @Override
    public IPage<UserRoleVO> queryByPage(UserRolePage page) {
        // 查询条件
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        // 查询分页数据
        Page<UserRole> userRolePage = new Page<UserRole>(page.getCurrent(), page.getSize());
        IPage<UserRole> pageData = baseMapper.selectPage(userRolePage, queryWrapper);

        //转换成vo
        IPage<UserRoleVO> records = PageCovertUtil.pageVoCovert(pageData, UserRoleVO.class);
        return records;
    }


    @Override
    public UserRoleVO queryById(Integer id) {
        UserRole userRole = baseMapper.selectById(id);
        return userRoleStructMapper.userRoleToUserRoleVO(userRole);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(UserRoleInsert userRoleInsert) {
        //先删除之前的关系，再添加新的用户和角色的关系
        remove(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId, userRoleInsert.getUserId()));
        //重新绑定
        if (CollUtil.isNotEmpty(userRoleInsert.getRoleIds())) {
            userRoleInsert.getRoleIds().forEach(r -> {
                UserRole userRole = new UserRole(userRoleInsert.getUserId(), r);
                save(userRole);
            });
        }
        //修改用户表的角色
        UserUpdate userUpdate = new UserUpdate();
        userUpdate.setId(userRoleInsert.getUserId());
        String s = userRoleInsert.getRoleIds().stream().map(String::valueOf).collect(Collectors.joining(","));
        userUpdate.setRoleIds(s);
        userService.update(userUpdate);
        return true;
    }


    @Override
    public boolean update(UserRoleUpdate userRoleUpdate) {
        UserRole userRole = userRoleStructMapper.updateToUserRole(userRoleUpdate);
        updateById(userRole);
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        return removeById(id);
    }

    @Override
    public List<Integer> getRoleIdsByUserId(Integer userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId, userId);
        List<Integer> collect = userRoleMapper.selectList(queryWrapper)
                .stream().map(UserRole::getRoleId).collect(Collectors.toList());
        return collect;
    }

}
